package com.xydtech.ecard.ftp.service.impl;

import com.xydtech.common.exception.ServiceException;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.ecard.ftp.domain.*;
import com.xydtech.ecard.ftp.enums.*;
import com.xydtech.ecard.ftp.service.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * @Author: liliya
 * @Description: 计算FTP贷款
 * @Date: Created in 2023/8/31 11:20
 * @Modified:
 */
@Service("ecardFtpCalculateFtpLoansService")
public class EcardFtpCalculateFtpLoansServiceImpl implements IEcardFtpCalculateFtpService {

    private final Logger logger = LogManager.getLogger(this.getClass());

    private IFtpDepositLoanRateConfigService ftpDepositLoanRateConfigService;
    private IFtpDepositLoanAdjustmentConfigService ftpDepositLoanAdjustmentConfigService;
    private IFtpPrincipalRepaymentPeriodService ftpPrincipalRepaymentPeriodService;
    private IFtpAverageCapitalPlusInterestService ftpAverageCapitalPlusInterestService;

    public EcardFtpCalculateFtpLoansServiceImpl(IFtpDepositLoanRateConfigService ftpDepositLoanRateConfigService,
                               IFtpDepositLoanAdjustmentConfigService ftpDepositLoanAdjustmentConfigService,
                               IFtpPrincipalRepaymentPeriodService ftpPrincipalRepaymentPeriodService,
                               IFtpAverageCapitalPlusInterestService ftpAverageCapitalPlusInterestService
    ) {
        this.ftpDepositLoanRateConfigService = ftpDepositLoanRateConfigService;
        this.ftpDepositLoanAdjustmentConfigService = ftpDepositLoanAdjustmentConfigService;
        this.ftpPrincipalRepaymentPeriodService = ftpPrincipalRepaymentPeriodService;
        this.ftpAverageCapitalPlusInterestService = ftpAverageCapitalPlusInterestService;
    }

    /**
     * 计算FTP贷款的结果汇总信息
     */
    @Override
    public EcardFtpResultsSummaryBO getCalculateFtpResultsInfo(EcardApiFtpInfo ecardApiFtpInfo) {

        EcardFtpResultsSummaryBO ecardFtpResultsSummaryBO = new EcardFtpResultsSummaryBO();

        try {

            /**
             * 基础价格
             */
            BigDecimal basicPrice = new BigDecimal("0");
            /**
             * 还款方式
             * TODO 存款不填此项
             * 到期还本付息	    1	loan_repayment_mode_type
             * 到期还本定期结息	2	loan_repayment_mode_type
             * 等额本金	        3	loan_repayment_mode_type
             * 等额本息	        4	loan_repayment_mode_type
             * 按还款计划还款	5	loan_repayment_mode_type
             */
            Integer loanRepaymentModeType = ecardApiFtpInfo.getLoanRepaymentModeType();
            //到期还本定期结息
            if (loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE2.getValue() ||
                    //到期还本付息
                    loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE1.getValue()) {
                EcardFtpDeadlineMatchBO summaryBO = ftpPrincipalRepaymentPeriodService.getCalculateBasicPrice(ecardApiFtpInfo);
                //基础价格
                basicPrice = summaryBO.getBasicPrice();
                //利率(年利率)(暂无用,但是需记录)
                ecardFtpResultsSummaryBO.setCustomerRate(ecardApiFtpInfo.getCustomerRate());
                //用于业务期限内实现FTP利润总计
                //余额总计(后续未直接使用,而是初始化)
                ecardFtpResultsSummaryBO.setBalanceAccumulation(new BigDecimal("0"));
                logger.info("电子名片FTP计算[基础价格-到期还本定期结息]信息------------------------>{}", summaryBO);
            }
            //等额本金或等额本息
            else if (loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE3.getValue() ||
                    loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE4.getValue()) {
                EcardFtpAverageCapitalPlusInterestSummaryBO summaryBO = ftpAverageCapitalPlusInterestService.getCalculateBasicPrice(ecardApiFtpInfo);
                //基础价格
                basicPrice = summaryBO.getBasicPrice();
                //利率(年利率)(暂无用,但是需记录)
                ecardFtpResultsSummaryBO.setCustomerRate(summaryBO.getRate());
                //用于业务期限内实现FTP利润总计
                //余额总计
                ecardFtpResultsSummaryBO.setBalanceAccumulation(summaryBO.getBalanceAccumulation());
                logger.info("电子名片FTP计算[基础价格-等额本金或等额本息]信息------------------------>{}", summaryBO);
            }
            ecardFtpResultsSummaryBO.setBasicPrice(basicPrice);

            /**
             * 期限流动性调整
             * TODO 贷款
             * TODO 存款默认0
             */
            BigDecimal termLiquidityAdjustment = getCalculateTermLiquidityAdjustment(ecardApiFtpInfo);
            ecardFtpResultsSummaryBO.setTermLiquidityAdjustment(termLiquidityAdjustment);

            logger.info("电子名片FTP计算[期限流动性调整]信息------------------------>{}", ecardFtpResultsSummaryBO);

            /**
             * 结息频率调整
             * TODO 贷款
             * TODO 存款默认0
             */
            BigDecimal interestFrequencyAdjustment = getCalculateInterestFrequencyAdjustment(ecardApiFtpInfo);
            ecardFtpResultsSummaryBO.setInterestFrequencyAdjustment(interestFrequencyAdjustment);

            logger.info("电子名片FTP计算[结息频率调整]信息------------------------>{}", ecardFtpResultsSummaryBO);

            /**
             * 存款准备金调整
             * TODO 存款  存款后续再算
             * TODO 贷款默认0
             */
            BigDecimal depositReserveFundAdjustment = getCalculateDepositReserveFundAdjustment(ecardApiFtpInfo);
            ecardFtpResultsSummaryBO.setDepositReserveFundAdjustment(depositReserveFundAdjustment);

            logger.info("电子名片FTP计算[存款准备金调整]信息------------------------>{}", ecardFtpResultsSummaryBO);

            /**
             * 最终转移价格 = 基础价格 + 期限流动性调整 + 结息频率调整 + 存款准备金调整 + 定期存款调整-最终调整值0
             */
            BigDecimal finalTransferPrice = getCalculateFinalTransferPrice(ecardFtpResultsSummaryBO);
            ecardFtpResultsSummaryBO.setFinalTransferPrice(finalTransferPrice);

            logger.info("电子名片FTP计算[最终转移价格]信息------------------------>{}", ecardFtpResultsSummaryBO);

            /**
             * 对客价格
             * 对客价格 = 对客利率（年利率） * 100
             * TODO 注意百分号
             */
            BigDecimal customerPrice = getCalculateCustomerPrice(ecardApiFtpInfo);
            ecardFtpResultsSummaryBO.setCustomerPrice(customerPrice);

            logger.info("电子名片FTP计算[对客价格]信息------------------------>{}", ecardFtpResultsSummaryBO);

            /**
             * FTP利差
             * FTP利差 = IF(!贷款)?(最终转移价格 - 对客价格):(对客价格 - 最终转移价格)
             * TODO 包含存款暂时不测
             */
            BigDecimal ftpInterestMargin = getCalculateFtpInterestMargin(ecardApiFtpInfo, ecardFtpResultsSummaryBO);
            ecardFtpResultsSummaryBO.setFtpInterestMargin(ftpInterestMargin);

            logger.info("电子名片FTP计算[FTP利差]信息------------------------>{}", ecardFtpResultsSummaryBO);

            /**
             * 业务期限内实现FTP利润总计
             */
            BigDecimal ftpAggregateProfit = getCalculateFtpAggregateProfit(ecardApiFtpInfo, ecardFtpResultsSummaryBO);
            ecardFtpResultsSummaryBO.setFtpAggregateProfit(ftpAggregateProfit);

            logger.info("电子名片FTP计算[业务期限内实现FTP利润总计]信息------------------------>{}", ecardFtpResultsSummaryBO);

            /**
             * 年化FTP利润
             */
            BigDecimal ftpAnnualizedProfit = getCalculateFtpAnnualizedProfit(ecardApiFtpInfo, ecardFtpResultsSummaryBO);
            ecardFtpResultsSummaryBO.setFtpAnnualizedProfit(ftpAnnualizedProfit);

            logger.info("电子名片FTP计算[年化FTP利润]信息------------------------>{}", ecardFtpResultsSummaryBO);

        } catch (Exception e) {
            e.printStackTrace();
            //TODO 后续异常需要返回初始化0再处理
            //BigDecimal exceptionVal = new BigDecimal(0);
            //ecardFtpResultsSummaryBO.setBasicPrice(exceptionVal);
            //ecardFtpResultsSummaryBO.setTermLiquidityAdjustment(exceptionVal);
            //ecardFtpResultsSummaryBO.setInterestFrequencyAdjustment(exceptionVal);
            //ecardFtpResultsSummaryBO.setDepositReserveFundAdjustment(exceptionVal);
            //ecardFtpResultsSummaryBO.setFinalTransferPrice(exceptionVal);
            //ecardFtpResultsSummaryBO.setCustomerPrice(exceptionVal);
            //ecardFtpResultsSummaryBO.setFtpInterestMargin(exceptionVal);
            //ecardFtpResultsSummaryBO.setFtpAggregateProfit(exceptionVal);
            //ecardFtpResultsSummaryBO.setFtpAnnualizedProfit(exceptionVal);
            throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION12.getMessage());
        }
        return ecardFtpResultsSummaryBO;
    }


    /**
     * 计算(sheet:结果汇总)中的"期限流动性调整"公式值
     *
     * @param ecardApiFtpInfo
     * @return
     */
    private BigDecimal getCalculateTermLiquidityAdjustment(EcardApiFtpInfo ecardApiFtpInfo) {
        /**
         * 期限流动性调整
         */
        BigDecimal termLiquidityAdjustment = new BigDecimal("0");

        /**
         * 业务品种
         * 贷款	    1	ftp_business_variety
         * 定期存款	2	ftp_business_variety
         * 个人活期	3	ftp_business_variety
         * 单位活期	4	ftp_business_variety
         */
        Integer ftpBusinessVariety = ecardApiFtpInfo.getFtpBusinessVariety();
        //TODO 存款默认0
        if (ftpBusinessVariety != FtpBusinessVarietyEnum.LOANS.getValue()) {
            return termLiquidityAdjustment;
        }

        //点差1
        BigDecimal spread01 = new BigDecimal("0");
        //点差2
        BigDecimal spread02 = new BigDecimal("0");

        /**
         * 利率类型
         * 固定	1	interest_rate_type
         * 浮动	2	interest_rate_type
         */
        //利率类型-1固定部分
        Integer interestRateType = ecardApiFtpInfo.getInterestRateType();
        if (interestRateType == FtpInterestRateTypeEnum.FIXED_RATE.getValue()) {
            termLiquidityAdjustment = new BigDecimal("0");
            return termLiquidityAdjustment;
        }

        //利率类型-2浮动部分
        //重定价周期(EcardApiFtpInfo-重定价频率(月))
        Integer repricingPeriod = ecardApiFtpInfo.getRepricingFrequencyType();
        if (repricingPeriod == FtpRepricingFrequencyTypeEnum.FREQUENCY_3M.getValue()) {
            //点差1
            FtpDepositLoanRateConfig ftpDepositLoanRateConfig1Y = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                    FtpDepositLoanRateConfigKeyEnum.Y_1.getConfigKey(),
                    FtpPricingParamTypeEnum.DEPOSIT_LOAD_YIELD_CURVE.getValue());
            FtpDepositLoanRateConfig ftpDepositLoanRateConfig3M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                    FtpDepositLoanRateConfigKeyEnum.M_3.getConfigKey(),
                    FtpPricingParamTypeEnum.DEPOSIT_LOAD_YIELD_CURVE.getValue());

            if (ftpDepositLoanRateConfig1Y != null && ftpDepositLoanRateConfig3M != null) {

                spread01 = new BigDecimal(ftpDepositLoanRateConfig1Y.getConfigRate())
                        .subtract(new BigDecimal(ftpDepositLoanRateConfig3M.getConfigRate()));
            }

            //点差2

            Date calculateDateBeginDate = ecardApiFtpInfo.getBeginDate();
            Date calculateDateEndDate = ecardApiFtpInfo.getEndDate();

            //业务基本信息:业务到期日 - 业务基本信息:业务起始日   TODO 多次使用待抽离
            Integer dateDiff = DateUtils.differentDaysByActualMillisecond(calculateDateBeginDate, calculateDateEndDate);
            if (dateDiff < 0) {
                return termLiquidityAdjustment;
            }

            String calculateDate1Str = DateUtils.dateTime(calculateDateBeginDate);

            Map<String, Integer> getDiffYearMonth01 = DateUtils.getDateDetails(calculateDateBeginDate, calculateDateEndDate);
            Integer getDiffYearMonth01Year = getDiffYearMonth01.get("years");
            Integer getDiffYearMonth01Days = getDiffYearMonth01.get("days");

            Integer date1YearInt = Integer.parseInt(calculateDate1Str.split("-")[0]);

            int leafYearNum = 0;
            for (int i = 0; i <= getDiffYearMonth01Year; i++) {
                //System.out.println(date1YearInt + i);
                Integer newDateYear = (date1YearInt + i);
                if ((newDateYear % 4 == 0 && newDateYear % 100 != 0) || newDateYear % 400 == 0) {
                    leafYearNum++;
                    //System.out.println( " newDateYear: " + newDateYear + "是闰年,第:" + leafYearNum + "个闰年!");
                }
            }

            //int leafYearNum = 0;
            //for (int i = 1; i <= getDiffYearMonth01Year; i++) {
            //    //System.out.println(date1YearInt + i);
            //    Integer newDateYear = (date1YearInt + i);
            //    if(i != getDiffYearMonth01Year){
            //        if ((newDateYear % 4 == 0 && newDateYear % 100 != 0) || newDateYear % 400 == 0) {
            //            leafYearNum++;
            //            //System.out.println( " newDateYear: " + newDateYear + "是闰年,第:" + leafYearNum + "个闰年!");
            //        }
            //    }
            //}

            Integer getDiffYearMonth01DaysCheck = ((getDiffYearMonth01Year * 365) + leafYearNum);
            boolean dateCheckDayFlag = (getDiffYearMonth01Days > getDiffYearMonth01DaysCheck);
            //if(dateCheckDayFlag){
            //    System.out.println("超出天数!");
            //}

            if (getDiffYearMonth01Year >= 1) {
                if (getDiffYearMonth01Year >= 5 && dateCheckDayFlag) {
                    //desc = "〉5Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_04.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year > 3 && getDiffYearMonth01Year <= 5) {
                    //desc = "（3，5]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_03.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year >= 3 && dateCheckDayFlag) {
                    //desc = "（3，5]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_03.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year > 2 && getDiffYearMonth01Year <= 3) {
                    //desc = "（2，3]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_02.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year >= 2 && dateCheckDayFlag) {
                    //desc = "（2，3]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_02.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year > 1 && getDiffYearMonth01Year <= 2) {
                    //desc = "（1，2]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_01.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year >= 1 && dateCheckDayFlag) {
                    //desc = "（1，2]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_01.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year == 1) {
                    //desc = "（6，12]M";
                    FtpDepositLoanRateConfig ftpDepositLoanRateConfig_6M12M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                            FtpDepositLoanRateConfigKeyEnum.M_6_12.getConfigKey(),
                            FtpPricingParamTypeEnum.TERM_LIQUIDITY_PREMIUM_SPREAD2.getValue());
                    spread02 = new BigDecimal(ftpDepositLoanRateConfig_6M12M.getConfigRate());
                }
            } else if (getDiffYearMonth01Year < 1) {

                //System.out.println("getDiffYearMonth01Days----------------- : " + getDiffYearMonth01Days);

                Date calculateDate03 = DateUtils.getNextMonth(calculateDateBeginDate, 3);
                Map<String, Integer> calculateDate03Map = DateUtils.getDateDetails(calculateDateBeginDate, calculateDate03);
                Integer calculateDate03MapDays = calculateDate03Map.get("days");
                //System.out.println("calculateDate03MapDays days : " + calculateDate03MapDays);

                Date calculateDate01 = DateUtils.getNextMonth(calculateDateBeginDate, 6);
                Map<String, Integer> calculateDate01Map = DateUtils.getDateDetails(calculateDateBeginDate, calculateDate01);
                Integer calculateDate01MapDays = calculateDate01Map.get("days");
                //System.out.println("calculateDate01Map days : " + calculateDate01MapDays);

                Date calculateDate02 = DateUtils.getNextMonth(calculateDateBeginDate, 12);
                Map<String, Integer> calculateDate02Map = DateUtils.getDateDetails(calculateDateBeginDate, calculateDate02);
                Integer calculateDate02MapDays = calculateDate02Map.get("days");
                //System.out.println("calculateDate02MapDays days : " + calculateDate02MapDays);
                if (getDiffYearMonth01Days > calculateDate01MapDays && getDiffYearMonth01Days <= calculateDate02MapDays) {
                    //6
                    FtpDepositLoanRateConfig ftpDepositLoanRateConfig_6M12M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                            FtpDepositLoanRateConfigKeyEnum.M_6_12.getConfigKey(),
                            FtpPricingParamTypeEnum.TERM_LIQUIDITY_PREMIUM_SPREAD2.getValue());
                    if (ftpDepositLoanRateConfig_6M12M != null) {
                        spread02 = new BigDecimal(ftpDepositLoanRateConfig_6M12M.getConfigRate());
                    } else {
                        spread02 = new BigDecimal("0");
                    }

                }
                //TODO 三个实时计算的三个月动态值是92/91,但是"sheet-贷款调整项"中的值,它里面写的是90,这里改成90。
                else if (getDiffYearMonth01Days > FtpLoanAdjustmentItemEnum.LOANS_3M6M_90.getValue() && getDiffYearMonth01Days <= calculateDate01MapDays) {
                    //3
                    FtpDepositLoanRateConfig ftpDepositLoanRateConfig_3M6M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                            FtpDepositLoanRateConfigKeyEnum.M_3_6.getConfigKey(),
                            FtpPricingParamTypeEnum.TERM_LIQUIDITY_PREMIUM_SPREAD2.getValue());
                    if (ftpDepositLoanRateConfig_3M6M != null) {
                        FtpDepositLoanRateConfig ftpDepositLoanRateConfig6M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                                FtpDepositLoanRateConfigKeyEnum.M_6.getConfigKey(),
                                FtpPricingParamTypeEnum.DEPOSIT_LOAD_YIELD_CURVE.getValue());
                        if (ftpDepositLoanRateConfig6M != null) {
                            spread02 = new BigDecimal(ftpDepositLoanRateConfig6M.getConfigRate()).subtract(new BigDecimal(ftpDepositLoanRateConfig1Y.getConfigRate()));
                        } else {
                            spread02 = new BigDecimal("0");
                        }
                    } else {
                        spread02 = new BigDecimal("0");
                    }

                } else {
                    spread02 = new BigDecimal("0");
                }
            }

            termLiquidityAdjustment = spread01.add(spread02).setScale(4, BigDecimal.ROUND_HALF_UP);


        } else if (repricingPeriod == FtpRepricingFrequencyTypeEnum.FREQUENCY_6M.getValue()) {
            //点差1
            FtpDepositLoanRateConfig ftpDepositLoanRateConfig1Y = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                    FtpDepositLoanRateConfigKeyEnum.Y_1.getConfigKey(),
                    FtpPricingParamTypeEnum.DEPOSIT_LOAD_YIELD_CURVE.getValue());
            FtpDepositLoanRateConfig ftpDepositLoanRateConfig6M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                    FtpDepositLoanRateConfigKeyEnum.M_6.getConfigKey(),
                    FtpPricingParamTypeEnum.DEPOSIT_LOAD_YIELD_CURVE.getValue());

            if (ftpDepositLoanRateConfig1Y != null && ftpDepositLoanRateConfig6M != null) {
                spread01 = new BigDecimal(ftpDepositLoanRateConfig1Y.getConfigRate()).subtract(new BigDecimal(ftpDepositLoanRateConfig6M.getConfigRate()));
            }

            //点差2
            //业务基本信息:业务到期日 - 业务基本信息:业务起始日   TODO 多次使用待抽离

            Date calculateDateBeginDate = ecardApiFtpInfo.getBeginDate();
            Date calculateDateEndDate = ecardApiFtpInfo.getEndDate();

            //业务基本信息:业务到期日 - 业务基本信息:业务起始日   TODO 多次使用待抽离
            Integer dateDiff = DateUtils.differentDaysByActualMillisecond(calculateDateBeginDate, calculateDateEndDate);
            if (dateDiff < 0) {
                return termLiquidityAdjustment;
            }

            String calculateDate1Str = DateUtils.dateTime(calculateDateBeginDate);

            Map<String, Integer> getDiffYearMonth01 = DateUtils.getDateDetails(calculateDateBeginDate, calculateDateEndDate);
            Integer getDiffYearMonth01Year = getDiffYearMonth01.get("years");
            Integer getDiffYearMonth01Days = getDiffYearMonth01.get("days");

            Integer date1YearInt = Integer.parseInt(calculateDate1Str.split("-")[0]);

            int leafYearNum = 0;
            for (int i = 0; i <= getDiffYearMonth01Year; i++) {
                //System.out.println(date1YearInt + i);
                Integer newDateYear = (date1YearInt + i);
                if ((newDateYear % 4 == 0 && newDateYear % 100 != 0) || newDateYear % 400 == 0) {
                    leafYearNum++;
                    //System.out.println( " newDateYear: " + newDateYear + "是闰年,第:" + leafYearNum + "个闰年!");
                }
            }

            //int leafYearNum = 0;
            //for (int i = 1; i <= getDiffYearMonth01Year; i++) {
            //    //System.out.println(date1YearInt + i);
            //    Integer newDateYear = (date1YearInt + i);
            //    if(i != getDiffYearMonth01Year){
            //        if ((newDateYear % 4 == 0 && newDateYear % 100 != 0) || newDateYear % 400 == 0) {
            //            leafYearNum++;
            //            //System.out.println( " newDateYear: " + newDateYear + "是闰年,第:" + leafYearNum + "个闰年!");
            //        }
            //    }
            //}

            Integer getDiffYearMonth01DaysCheck = ((getDiffYearMonth01Year * 365) + leafYearNum);
            boolean dateCheckDayFlag = (getDiffYearMonth01Days > getDiffYearMonth01DaysCheck);
            //if(dateCheckDayFlag){
            //    System.out.println("超出天数!");
            //}

            if (getDiffYearMonth01Year >= 1) {
                if (getDiffYearMonth01Year >= 5 && dateCheckDayFlag) {
                    //desc = "〉5Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_04.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year > 3 && getDiffYearMonth01Year <= 5) {
                    //desc = "（3，5]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_03.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year >= 3 && dateCheckDayFlag) {
                    //desc = "（3，5]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_03.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year > 2 && getDiffYearMonth01Year <= 3) {
                    //desc = "（2，3]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_02.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year >= 2 && dateCheckDayFlag) {
                    //desc = "（2，3]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_02.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year > 1 && getDiffYearMonth01Year <= 2) {
                    //desc = "（1，2]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_01.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year >= 1 && dateCheckDayFlag) {
                    //desc = "（1，2]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_01.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year == 1) {
                    //desc = "（6，12]M";
                    FtpDepositLoanRateConfig ftpDepositLoanRateConfig_6M12M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                            FtpDepositLoanRateConfigKeyEnum.M_6_12.getConfigKey(),
                            FtpPricingParamTypeEnum.TERM_LIQUIDITY_PREMIUM_SPREAD2.getValue());
                    spread02 = new BigDecimal(ftpDepositLoanRateConfig_6M12M.getConfigRate());
                }
            } else if (getDiffYearMonth01Year < 1) {

                //System.out.println("getDiffYearMonth01Days----------------- : " + getDiffYearMonth01Days);

                Date calculateDate03 = DateUtils.getNextMonth(calculateDateBeginDate, 3);
                Map<String, Integer> calculateDate03Map = DateUtils.getDateDetails(calculateDateBeginDate, calculateDate03);
                Integer calculateDate03MapDays = calculateDate03Map.get("days");
                //System.out.println("calculateDate03MapDays days : " + calculateDate03MapDays);

                Date calculateDate01 = DateUtils.getNextMonth(calculateDateBeginDate, 6);
                Map<String, Integer> calculateDate01Map = DateUtils.getDateDetails(calculateDateBeginDate, calculateDate01);
                Integer calculateDate01MapDays = calculateDate01Map.get("days");
                //System.out.println("calculateDate01Map days : " + calculateDate01MapDays);

                Date calculateDate02 = DateUtils.getNextMonth(calculateDateBeginDate, 12);
                Map<String, Integer> calculateDate02Map = DateUtils.getDateDetails(calculateDateBeginDate, calculateDate02);
                Integer calculateDate02MapDays = calculateDate02Map.get("days");
                //System.out.println("calculateDate02MapDays days : " + calculateDate02MapDays);

                if (getDiffYearMonth01Days > calculateDate01MapDays && getDiffYearMonth01Days <= calculateDate02MapDays) {
                    //6
                    FtpDepositLoanRateConfig ftpDepositLoanRateConfig_6M12M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                            FtpDepositLoanRateConfigKeyEnum.M_6_12.getConfigKey(),
                            FtpPricingParamTypeEnum.TERM_LIQUIDITY_PREMIUM_SPREAD2.getValue());
                    if (ftpDepositLoanRateConfig_6M12M != null) {
                        spread02 = new BigDecimal(ftpDepositLoanRateConfig_6M12M.getConfigRate());
                    } else {
                        spread02 = new BigDecimal("0");
                    }

                }
                //TODO 三个实时计算的三个月动态值是92/91,但是"sheet-贷款调整项"中的值,它里面写的是90,这里改成90。
                else if (getDiffYearMonth01Days > FtpLoanAdjustmentItemEnum.LOANS_3M6M_90.getValue() && getDiffYearMonth01Days <= calculateDate01MapDays) {
                    //3
                    FtpDepositLoanRateConfig ftpDepositLoanRateConfig_3M6M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                            FtpDepositLoanRateConfigKeyEnum.M_3_6.getConfigKey(),
                            FtpPricingParamTypeEnum.TERM_LIQUIDITY_PREMIUM_SPREAD2.getValue());
                    if (ftpDepositLoanRateConfig_3M6M != null) {
                        if (ftpDepositLoanRateConfig6M != null) {
                            spread02 = new BigDecimal(ftpDepositLoanRateConfig6M.getConfigRate()).subtract(new BigDecimal(ftpDepositLoanRateConfig1Y.getConfigRate()));
                        } else {
                            spread02 = new BigDecimal("0");
                        }
                    } else {
                        spread02 = new BigDecimal("0");
                    }

                } else {
                    spread02 = new BigDecimal("0");
                }
            }

            termLiquidityAdjustment = spread01.add(spread02).setScale(4, BigDecimal.ROUND_HALF_UP);

        } else if (repricingPeriod == FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()) {

            //点差2

            Date calculateDateBeginDate = ecardApiFtpInfo.getBeginDate();
            Date calculateDateEndDate = ecardApiFtpInfo.getEndDate();

            //业务基本信息:业务到期日 - 业务基本信息:业务起始日   TODO 多次使用待抽离
            Integer dateDiff = DateUtils.differentDaysByActualMillisecond(calculateDateBeginDate, calculateDateEndDate);
            if (dateDiff < 0) {
                return termLiquidityAdjustment;
            }

            String calculateDate1Str = DateUtils.dateTime(calculateDateBeginDate);

            Map<String, Integer> getDiffYearMonth01 = DateUtils.getDateDetails(calculateDateBeginDate, calculateDateEndDate);
            Integer getDiffYearMonth01Year = getDiffYearMonth01.get("years");
            Integer getDiffYearMonth01Days = getDiffYearMonth01.get("days");

            Integer date1YearInt = Integer.parseInt(calculateDate1Str.split("-")[0]);


            int leafYearNum = 0;
            for (int i = 0; i <= getDiffYearMonth01Year; i++) {
                //System.out.println(date1YearInt + i);
                Integer newDateYear = (date1YearInt + i);
                if ((newDateYear % 4 == 0 && newDateYear % 100 != 0) || newDateYear % 400 == 0) {
                    leafYearNum++;
                    //System.out.println( " newDateYear: " + newDateYear + "是闰年,第:" + leafYearNum + "个闰年!");
                }
            }

            //int leafYearNum = 0;
            //for (int i = 1; i <= getDiffYearMonth01Year; i++) {
            //    //System.out.println(date1YearInt + i);
            //    Integer newDateYear = (date1YearInt + i);
            //    if(i != getDiffYearMonth01Year){
            //        if ((newDateYear % 4 == 0 && newDateYear % 100 != 0) || newDateYear % 400 == 0) {
            //            leafYearNum++;
            //            //System.out.println( " newDateYear: " + newDateYear + "是闰年,第:" + leafYearNum + "个闰年!");
            //        }
            //    }
            //}

            Integer getDiffYearMonth01DaysCheck = ((getDiffYearMonth01Year * 365) + leafYearNum);
            boolean dateCheckDayFlag = (getDiffYearMonth01Days > getDiffYearMonth01DaysCheck);
            //if(dateCheckDayFlag){
            //    System.out.println("超出天数!");
            //}

            if (getDiffYearMonth01Year >= 1) {
                if (getDiffYearMonth01Year >= 5 && dateCheckDayFlag) {
                    //desc = "〉5Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_04.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year > 3 && getDiffYearMonth01Year <= 5) {
                    //desc = "（3，5]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_03.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year >= 3 && dateCheckDayFlag) {
                    //desc = "（3，5]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_03.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year > 2 && getDiffYearMonth01Year <= 3) {
                    //desc = "（2，3]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_02.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year >= 2 && dateCheckDayFlag) {
                    //desc = "（2，3]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_02.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year > 1 && getDiffYearMonth01Year <= 2) {
                    //desc = "（1，2]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_01.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year >= 1 && dateCheckDayFlag) {
                    //desc = "（1，2]Y";
                    FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig =
                            ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationInfo(
                                    String.valueOf(FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()),
                                    FtpSysDictTypeEnum.REPRICING_FREQUENCY_TYPE.getDictType(),
                                    FtpDateTypeEnum.Y.name().toUpperCase(),
                                    FtpRepricingFrequencyDeadlineTypeEnum.REPRICING_FREQUENCY_DEADLINE_TYPE_01.getOriginalDeadlineType());
                    spread02 = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate());
                } else if (getDiffYearMonth01Year == 1) {
                    //desc = "（6，12]M";
                    FtpDepositLoanRateConfig ftpDepositLoanRateConfig_6M12M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                            FtpDepositLoanRateConfigKeyEnum.M_6_12.getConfigKey(),
                            FtpPricingParamTypeEnum.TERM_LIQUIDITY_PREMIUM_SPREAD2.getValue());
                    spread02 = new BigDecimal(ftpDepositLoanRateConfig_6M12M.getConfigRate());
                }
            }


            else if (getDiffYearMonth01Year < 1) {


                Date calculateDate03 = DateUtils.getNextMonth(calculateDateBeginDate, 3);
                Map<String, Integer> calculateDate03Map = DateUtils.getDateDetails(calculateDateBeginDate, calculateDate03);
                Integer calculateDate03MapDays = calculateDate03Map.get("days");
                //System.out.println("calculateDate03MapDays days : " + calculateDate03MapDays);

                Date calculateDate01 = DateUtils.getNextMonth(calculateDateBeginDate, 6);
                Map<String, Integer> calculateDate01Map = DateUtils.getDateDetails(calculateDateBeginDate, calculateDate01);
                Integer calculateDate01MapDays = calculateDate01Map.get("days");
                //System.out.println("calculateDate01Map days : " + calculateDate01MapDays);

                Date calculateDate02 = DateUtils.getNextMonth(calculateDateBeginDate, 12);
                Map<String, Integer> calculateDate02Map = DateUtils.getDateDetails(calculateDateBeginDate, calculateDate02);
                Integer calculateDate02MapDays = calculateDate02Map.get("days");
                //System.out.println("calculateDate02MapDays days : " + calculateDate02MapDays);

                if (getDiffYearMonth01Days > calculateDate01MapDays && getDiffYearMonth01Days <= calculateDate02MapDays) {
                    //6
                    FtpDepositLoanRateConfig ftpDepositLoanRateConfig_6M12M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                            FtpDepositLoanRateConfigKeyEnum.M_6_12.getConfigKey(),
                            FtpPricingParamTypeEnum.TERM_LIQUIDITY_PREMIUM_SPREAD2.getValue());
                    if (ftpDepositLoanRateConfig_6M12M != null) {
                        spread02 = new BigDecimal(ftpDepositLoanRateConfig_6M12M.getConfigRate());
                    } else {
                        spread02 = new BigDecimal("0");
                    }

                }
                //TODO 三个实时计算的三个月动态值是92/91,但是"sheet-贷款调整项"中的值,它里面写的是90,这里改成90。
                else if (getDiffYearMonth01Days > FtpLoanAdjustmentItemEnum.LOANS_3M6M_90.getValue() && getDiffYearMonth01Days <= calculateDate01MapDays) {
                    //3
                    FtpDepositLoanRateConfig ftpDepositLoanRateConfig_3M6M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                            FtpDepositLoanRateConfigKeyEnum.M_3_6.getConfigKey(),
                            FtpPricingParamTypeEnum.TERM_LIQUIDITY_PREMIUM_SPREAD2.getValue());
                    if (ftpDepositLoanRateConfig_3M6M != null) {
                        FtpDepositLoanRateConfig ftpDepositLoanRateConfig6M = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                                FtpDepositLoanRateConfigKeyEnum.M_6.getConfigKey(),
                                FtpPricingParamTypeEnum.DEPOSIT_LOAD_YIELD_CURVE.getValue());
                        FtpDepositLoanRateConfig ftpDepositLoanRateConfig1Y = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(
                                FtpDepositLoanRateConfigKeyEnum.Y_1.getConfigKey(),
                                FtpPricingParamTypeEnum.DEPOSIT_LOAD_YIELD_CURVE.getValue());
                        if (ftpDepositLoanRateConfig6M != null && ftpDepositLoanRateConfig1Y != null) {
                            spread02 = new BigDecimal(ftpDepositLoanRateConfig6M.getConfigRate()).subtract(new BigDecimal(ftpDepositLoanRateConfig1Y.getConfigRate()));
                        } else {
                            spread02 = new BigDecimal("0");
                        }
                    } else {
                        spread02 = new BigDecimal("0");
                    }

                } else {
                    spread02 = new BigDecimal("0");
                }
            }

            termLiquidityAdjustment = spread01.add(spread02).setScale(4, BigDecimal.ROUND_HALF_UP);

        }

        logger.info("电子名片FTP计算[期限流动性调整]信息------------------点差1-spread01------>{},点差-spread02------>{}"
                , spread01, spread02);

        return termLiquidityAdjustment;
    }

    /**
     * 计算(sheet:结果汇总)中的"结息频率调整"公式值
     */
    private BigDecimal getCalculateInterestFrequencyAdjustment(EcardApiFtpInfo ecardApiFtpInfo) {
        /**
         * 结息频率调整
         */
        BigDecimal interestFrequencyAdjustment = new BigDecimal("0");
        /**
         * 业务品种
         * 贷款	    1	ftp_business_variety
         * 定期存款	2	ftp_business_variety
         * 个人活期	3	ftp_business_variety
         * 单位活期	4	ftp_business_variety
         */
        Integer ftpBusinessVariety = ecardApiFtpInfo.getFtpBusinessVariety();
        //TODO 存款默认0
        if (ftpBusinessVariety != FtpBusinessVarietyEnum.LOANS.getValue()) {
            return interestFrequencyAdjustment;
        }

        /**
         * 还款方式
         * TODO 存款不填此项
         * 到期还本付息	    1	loan_repayment_mode_type
         * 到期还本定期结息	2	loan_repayment_mode_type
         * 等额本金	        3	loan_repayment_mode_type
         * 等额本息	        4	loan_repayment_mode_type
         * 按还款计划还款	5	loan_repayment_mode_type
         */
        //TODO 到期还本付息与到期还本定期结息的唯一区别就是,到期还本付息的结息频率调整值默认0!
        Integer loanRepaymentModeType = ecardApiFtpInfo.getLoanRepaymentModeType();
        if (loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE1.getValue()) {
            return interestFrequencyAdjustment;
        }

        /**
         * 结息频率(月)
         * TODO 当还款方式为到期还本付息和业务品种ftpBusinessVariety为存款不填此项
         * 1M	    1	interest_frequency_type
         * 3M	    3	interest_frequency_type
         */
        Integer interestFrequencyType = ecardApiFtpInfo.getInterestFrequencyType();
        //日期差值
        Integer dateDiff = DateUtils.differentDaysByActualMillisecond(ecardApiFtpInfo.getBeginDate(), ecardApiFtpInfo.getEndDate());
        if (dateDiff < 0) {
            return interestFrequencyAdjustment;
        }
        /**
         * 通过期限匹配法-结息频率调整项:定价期限(天数上限,闭区间)和config_key查对应区间
         * @param dictValue     sys_dict_data   1/3
         * @param dictType      sys_dict_data   interest_frequency_type
         * @param unit          ftp_deposit_loan_adjustment_config  单位Y年
         * @param calcuDay      ftp_deposit_loan_adjustment_config的calcu_under_day与calcu_upper_day差值
         * @return
         */
        FtpDepositLoanAdjustmentConfig ftpDepositLoanAdjustmentConfig = ftpDepositLoanAdjustmentConfigService.selectBaseListOfSysRelationUnderClose(
                String.valueOf(interestFrequencyType),
                FtpSysDictTypeEnum.INTEREST_FREQUENCY_TYPE.getDictType(),
                FtpDateTypeEnum.Y.name().toUpperCase(),
                dateDiff);

        //System.out.println("3-------------------------结息频率调整--:" + ftpDepositLoanAdjustmentConfig);

        logger.info("电子名片FTP计算[结息频率调整]信息------------------->{}", ftpDepositLoanAdjustmentConfig);
        if (interestFrequencyAdjustment == null) {
            return interestFrequencyAdjustment;
        }
        interestFrequencyAdjustment = new BigDecimal(ftpDepositLoanAdjustmentConfig.getAdjustmentRate()).setScale(4, BigDecimal.ROUND_HALF_UP);

        return interestFrequencyAdjustment;
    }

    /**
     * 计算(sheet:结果汇总)中的"存款准备金调整"公式值
     */
    private BigDecimal getCalculateDepositReserveFundAdjustment(EcardApiFtpInfo ecardApiFtpInfo) {

        BigDecimal depositReserveFundAdjustment = new BigDecimal("0");
        /**
         * 业务品种
         * 贷款	    1	ftp_business_variety
         * 定期存款	2	ftp_business_variety
         * 个人活期	3	ftp_business_variety
         * 单位活期	4	ftp_business_variety
         */
        Integer ftpBusinessVariety = ecardApiFtpInfo.getFtpBusinessVariety();
        //TODO 贷款默认0
        if (ftpBusinessVariety == FtpBusinessVarietyEnum.LOANS.getValue()) {
            return depositReserveFundAdjustment.setScale(4, BigDecimal.ROUND_HALF_UP);
        }
        //TODO 存款暂时不写
        return depositReserveFundAdjustment.setScale(4, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 计算(sheet:结果汇总)中的"最终转移价格"公式值
     */
    private BigDecimal getCalculateFinalTransferPrice(EcardFtpResultsSummaryBO ecardFtpResultsSummaryBO) {
        /**
         * 最终转移价格 = 基础价格 + 期限流动性调整 + 结息频率调整 + 存款准备金调整 + 定期存款调整-最终调整值0
         */
        BigDecimal finalTransferPrice = ecardFtpResultsSummaryBO.getBasicPrice()
                .add(ecardFtpResultsSummaryBO.getTermLiquidityAdjustment())
                .add(ecardFtpResultsSummaryBO.getInterestFrequencyAdjustment())
                .add(ecardFtpResultsSummaryBO.getDepositReserveFundAdjustment())
                .add(new BigDecimal("0"))
                .setScale(4, BigDecimal.ROUND_HALF_UP);

        return finalTransferPrice;
    }

    /**
     * 计算(sheet:结果汇总)中的"对客价格"公式值
     * 对客价格 = 对客利率（年利率） * 100
     * TODO 注意百分号
     */
    private BigDecimal getCalculateCustomerPrice(EcardApiFtpInfo ecardApiFtpInfo) {

        BigDecimal customerPrice = ecardApiFtpInfo.getCustomerRate()
                .divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP)
                .multiply(new BigDecimal(100))
                .setScale(4, BigDecimal.ROUND_HALF_UP);

        return customerPrice;
    }

    /**
     * 计算(sheet:结果汇总)中的"FTP利差"公式值
     * FTP利差 = IF(!贷款)?(最终转移价格 - 对客价格):(对客价格 - 最终转移价格)
     */
    private BigDecimal getCalculateFtpInterestMargin(EcardApiFtpInfo ecardApiFtpInfo, EcardFtpResultsSummaryBO ecardFtpResultsSummaryBO) {

        BigDecimal ftpInterestMargin;

        /**
         * 最终转移价格
         */
        BigDecimal finalTransferPrice = ecardFtpResultsSummaryBO.getFinalTransferPrice();
        /**
         * 对客价格
         */
        BigDecimal customerPrice = ecardFtpResultsSummaryBO.getCustomerPrice();
        /**
         * 业务品种
         * 贷款	    1	ftp_business_variety
         * 定期存款	2	ftp_business_variety
         * 个人活期	3	ftp_business_variety
         * 单位活期	4	ftp_business_variety
         */
        Integer ftpBusinessVariety = ecardApiFtpInfo.getFtpBusinessVariety();
        //贷款
        if (ftpBusinessVariety == FtpBusinessVarietyEnum.LOANS.getValue()) {
            ftpInterestMargin = customerPrice.subtract(finalTransferPrice);
        }
        //TODO 存款   暂时不测
        else {
            ftpInterestMargin = finalTransferPrice.subtract(customerPrice);
        }
        return ftpInterestMargin.setScale(4, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 计算(sheet:结果汇总)中的"业务期限内实现FTP利润总计"公式值
     * 业务期限内实现FTP利润总计 = (FTP利差 * 余额累计)/360/100;
     */
    private BigDecimal getCalculateFtpAggregateProfit(EcardApiFtpInfo ecardApiFtpInfo, EcardFtpResultsSummaryBO ecardFtpResultsSummaryBO) {

        BigDecimal ftpAggregateProfit = new BigDecimal("0");

        /**
         * TODO 不同还款方式对应不同的定价方法,需要做对应关系并且抽离公共方法.
         * 业务品种
         * 贷款	    1	ftp_business_variety
         * 定期存款	2	ftp_business_variety
         * 个人活期	3	ftp_business_variety
         * 单位活期	4	ftp_business_variety
         */
        Integer ftpBusinessVariety = ecardApiFtpInfo.getFtpBusinessVariety();

        //贷款
        if (ftpBusinessVariety == FtpBusinessVarietyEnum.LOANS.getValue()) {

            /**
             * 还款方式
             * TODO 存款不填此项
             * 到期还本付息	    1	loan_repayment_mode_type
             * 到期还本定期结息	2	loan_repayment_mode_type
             * 等额本金	        3	loan_repayment_mode_type
             * 等额本息	        4	loan_repayment_mode_type
             * 按还款计划还款	5	loan_repayment_mode_type
             */
            Integer loanRepaymentModeType = ecardApiFtpInfo.getLoanRepaymentModeType();
            //到期还本付息和到期还本定期结息
            if (loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE1.getValue() ||
                    loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE2.getValue()) {
                Integer dateDiff = DateUtils.differentDaysByActualMillisecond(ecardApiFtpInfo.getBeginDate(), ecardApiFtpInfo.getEndDate());
                //余额累计 = (业务基本信息:业务金额(元)) * (业务基本信息:业务到期日 - 业务基本信息:业务起始日)
                BigDecimal cumulativeBalances = ecardApiFtpInfo.getBusinessAmount().multiply(new BigDecimal(String.valueOf(dateDiff)));
                ecardFtpResultsSummaryBO.setBalanceAccumulation(cumulativeBalances);
                //FTP利差
                BigDecimal ftpInterestMargin = ecardFtpResultsSummaryBO.getFtpInterestMargin();
                //(FTP利差 * 余额累计)
                BigDecimal multiplyVal01 = ftpInterestMargin.multiply(ecardFtpResultsSummaryBO.getBalanceAccumulation());
                //360 * 100
                BigDecimal multiplyVal02 = new BigDecimal(360).multiply(new BigDecimal(100));
                //业务期限内实现FTP利润总计 = (FTP利差 * 余额累计)/360/100;
                ftpAggregateProfit = multiplyVal01.divide(multiplyVal02, 2, BigDecimal.ROUND_HALF_UP);
            }
            //等额本金/等额本息
            else if (loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE3.getValue() ||
                    loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE4.getValue()) {

                //余额累计
                BigDecimal cumulativeBalances = ecardFtpResultsSummaryBO.getBalanceAccumulation();
                //FTP利差
                BigDecimal ftpInterestMargin = ecardFtpResultsSummaryBO.getFtpInterestMargin();
                //(FTP利差 * 余额累计)
                BigDecimal multiplyVal01 = ftpInterestMargin.multiply(cumulativeBalances);
                //360 * 100
                BigDecimal multiplyVal02 = new BigDecimal(360).multiply(new BigDecimal(100));
                //业务期限内实现FTP利润总计 = (FTP利差 * 余额累计)/360/100;
                ftpAggregateProfit = multiplyVal01.divide(multiplyVal02, 2, BigDecimal.ROUND_HALF_UP);

            }
            //按还款计划还款
            else if (loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE5.getValue()) {

            }

        }
        return ftpAggregateProfit;
    }

    /**
     * 计算(sheet:结果汇总)中的"年化FTP利润"公式值
     * (业务期限内实现FTP利润总计/日期差值) * 365
     */
    private BigDecimal getCalculateFtpAnnualizedProfit(EcardApiFtpInfo ecardApiFtpInfo, EcardFtpResultsSummaryBO ecardFtpResultsSummaryBO) {

        //业务期限内实现FTP利润总计
        BigDecimal ftpAggregateProfit = ecardFtpResultsSummaryBO.getFtpAggregateProfit();
        //业务基本信息:业务到期日 - 业务基本信息:业务起始日   TODO 多次使用待抽离
        Integer dateDiff = DateUtils.differentDaysByActualMillisecond(ecardApiFtpInfo.getBeginDate(), ecardApiFtpInfo.getEndDate());

        BigDecimal multiplyVal01 = ftpAggregateProfit.multiply(new BigDecimal("365"));
        BigDecimal ftpAnnualizedProfit = multiplyVal01.divide(new BigDecimal(dateDiff), 2, BigDecimal.ROUND_HALF_UP);

        return ftpAnnualizedProfit;
    }


    public IFtpDepositLoanRateConfigService getFtpDepositLoanRateConfigService() {
        return ftpDepositLoanRateConfigService;
    }

    public void setFtpDepositLoanRateConfigService(IFtpDepositLoanRateConfigService ftpDepositLoanRateConfigService) {
        this.ftpDepositLoanRateConfigService = ftpDepositLoanRateConfigService;
    }

    public IFtpDepositLoanAdjustmentConfigService getFtpDepositLoanAdjustmentConfigService() {
        return ftpDepositLoanAdjustmentConfigService;
    }

    public void setFtpDepositLoanAdjustmentConfigService(IFtpDepositLoanAdjustmentConfigService
                                                                 ftpDepositLoanAdjustmentConfigService) {
        this.ftpDepositLoanAdjustmentConfigService = ftpDepositLoanAdjustmentConfigService;
    }

    public IFtpPrincipalRepaymentPeriodService getFtpPrincipalRepaymentPeriodService() {
        return ftpPrincipalRepaymentPeriodService;
    }

    public void setFtpPrincipalRepaymentPeriodService(IFtpPrincipalRepaymentPeriodService
                                                              ftpPrincipalRepaymentPeriodService) {
        this.ftpPrincipalRepaymentPeriodService = ftpPrincipalRepaymentPeriodService;
    }

    public IFtpAverageCapitalPlusInterestService getFtpAverageCapitalPlusInterestService() {
        return ftpAverageCapitalPlusInterestService;
    }

    public void setFtpAverageCapitalPlusInterestService(IFtpAverageCapitalPlusInterestService ftpAverageCapitalPlusInterestService) {
        this.ftpAverageCapitalPlusInterestService = ftpAverageCapitalPlusInterestService;
    }
}
